package com.love.cloud.common.security.component;

import com.love.cloud.common.core.constant.CacheConstants;
import com.love.cloud.common.security.properties.MySecurityTokenProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * @author lengleng
 * @date 2020/9/29
 * <p>
 * redis token store 自动配置
 */
@RequiredArgsConstructor
@Configuration(proxyBeanMethods = false)
public class MyTokenStoreAutoConfiguration {
	private final RedisConnectionFactory redisConnectionFactory;
	private final MySecurityTokenProperties mySecurityTokenProperties;

	@Bean
	public TokenStore tokenStore() {
		RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
		tokenStore.setPrefix(CacheConstants.PROJECT_OAUTH_ACCESS);
		//设置自定义token生成的规则
		tokenStore.setAuthenticationKeyGenerator(CustomAuthGeneratorFactory.getGenerator(mySecurityTokenProperties.getUserOnlyTokenGenerator()));
		return tokenStore;
	}

}
